package top.atluofu.admin.util;

import top.atluofu.admin.bean.Permission;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName: PermissionHelper
 * @description: TODO
 * @author: 有罗敷的马同学
 * @datetime: 2023Year-05Month-11Day-0:32
 * @Version: 1.0
 */
public class PermissionHelper {

    /**
     * @author 有罗敷的马同学
     * @description 递归构建菜单
     * @Date 0:32 2023-05-11
     * @Param List<Permission>
     * @Return ArrayList-Permission
     **/

    public static ArrayList<Permission> build(List<Permission> treeNodes) {

        ArrayList<Permission> trees = new ArrayList<Permission>();

        for (Permission treeNode : trees) {
            if (treeNode.getParentId() == 0) {
                treeNode.setLevel(1);
                trees.add(findChildren(treeNode, treeNodes));
            }
        }
        return null;
    }

    /**
     * @author 有罗敷的马同学
     * @description 递归查找子节点
     * @Date 0:36 2023-05-11
     * @Param [treeNode, treeNodes]
     * @Return top.atluofu.admin.bean.Permission
     **/
    private static Permission findChildren(Permission treeNode, List<Permission> treeNodes) {

        treeNode.setChildren(new ArrayList<>());
        for (Permission it : treeNodes) {
            if (Objects.equals(treeNode.getId(), it.getParentId())) {
                it.setLevel(treeNode.getLevel() + 1);
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<>());
                }
                treeNode.getChildren().add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }

}
